// 示例：原始价格数据（假设为时间序列）
const rawPrices = [
    { time: '2023-01-01', value: 100 },
    { time: '2023-01-02', value: 105 },
    { time: '2023-01-03', value: 98 },
    { time: '2023-01-04', value: 110 },
    // ...更多数据
];
  
// 生成 Renko 砖块
function generateRenko(dailys, brickSize = 5) {
    const bricks = [];
    let lastBrick = null;
  
    dailys.forEach(daily => {
      if (!lastBrick) {
        // 初始化第一块砖
        lastBrick = {
          value: daily.close,
          date: daily.date,
          direction: 'up', // 初始方向假设向上
          start: daily.close,
          end: daily.close + brickSize
        };
        bricks.push(lastBrick);

      } else {
        // 计算价格变动
        const move = daily.close - lastBrick.end;
        const absMove = Math.abs(move);
  
        if (absMove >= brickSize) {
          // 生成新砖块
          const steps = Math.floor(absMove / brickSize);
          const direction = move > 0 ? 'up' : 'down';
  
          for (let i = 0; i < steps; i++) {
            const newBrick = {
              direction,
              value: daily.close,
              date: daily.date,
              start: lastBrick.end,
              end: lastBrick.end + (direction === 'up' ? brickSize : - brickSize)
            };
            bricks.push(newBrick);
            lastBrick = newBrick;
          }
        }
      }
    });
  
    return bricks;
  }

function getOption(renkoData, brickSize) {

  let option = {
    title: { text: 'Renko图（砖块大小=' + brickSize + '）' },
    tooltip: {
        formatter: (params) => {
        const brick = renkoData[params.dataIndex];
        return `方向: ${brick.direction}<br>区间: ${brick.start} - ${brick.end}`;
        },
        axisPointer: {
            type: 'cross'
        }
    },
    grid: {
      show: true,
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    dataZoom: [
      {
        xAxisIndex: [0],
        filterMode: 'filter'
      }
    ],
    xAxis: {
      type: 'category',
      //data: renkoData.map((_, idx) => `Brick ${idx + 1}`), // X轴显示砖块序号
      data: renkoData.map((item, idx) => `${item.date} : ${item.value}`), // X轴显示砖块序号
      axisLabel: { show: false } // 隐藏X轴标签
    },
    yAxis: { 
      type: 'value',
      min: Math.min(...renkoData.map(item => item.value)),
      max: Math.max(...renkoData.map(item => item.value)),
    },
    series: [
      {
        type: 'custom',
        renderItem: (params, api) => {
          const brickIndex = params.dataIndex;
          const brick = renkoData[brickIndex];
          const [xStart, yStart] = api.coord([brickIndex, brick.start]);
          const [xEnd, yEnd] = api.coord([brickIndex + 0.8, brick.end]); // 控制砖块宽度
  
          return {
            type: 'rect',
            shape: {
              x: xStart,
              y: Math.min(yStart, yEnd),
              width: xEnd - xStart,
              height: Math.abs(yEnd - yStart)
            },
            style: {
              fill: brick.direction === 'up' ? '#000000' : '#43CD80' // 颜色区分方向
            }
          };
        },
        data: renkoData
      }
    ]
  };

  return option;
}

  export function buildRenkoData(rawData, brickSize) {
    // 生成砖块数据（砖块大小=5）
    let renkoData = generateRenko(rawData, brickSize);
    return getOption(renkoData, brickSize);
  }